<?php
require_once ('../common/mysql/mysqlidb.php');
require_once('../common/mysql/mysql.config.php');
require_once('../common/mysql/tables.config.php');

function add_event_redirct_info($info, $location) {
    echo <<<EOT
        <meta charset='utf-8'>
        <script type="text/javascript">
        alert("$info");
        location.href = "$location";
        </script>
EOT;
}

$db = new \MysqliDb(MYSQL_HOSTADDR, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DB_CALENDAR, MYSQL_PORT, CALENDAR_TABLE_PREFIX);
//var_dump($_REQUEST);

$user_openid = @$_REQUEST['user_openid'];
$day_title = @$_REQUEST['day_title'];
$recycle = @$_REQUEST['recycle'];
$day_desc  = @$_REQUEST['day_desc'];
$date_format = @$_REQUEST['date_format'];

$remind_year = @$_REQUEST['time_year'];
$remind_month = @$_REQUEST['time_month'];
$remind_day = @$_REQUEST['time_day'];


if($user_openid == "")
{
    add_event_redirct_info("添加纪念日出错了，请从公众号页面重新进入！", "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxab31157c01de5423&redirect_uri=http://hmtbzx.com/calendar/view/addevent.php&response_type=code&scope=snsapi_base&state=123&connect_redirect=1#wechat_redirect");

    return;
}

if($day_title == "")
{
    add_event_redirct_info("添加纪念日出错了，标题必须填写！", "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxab31157c01de5423&redirect_uri=http://hmtbzx.com/calendar/view/addevent.php&response_type=code&scope=snsapi_base&state=123&connect_redirect=1#wechat_redirect");

    return;
}

$special_day = Array(
    'account' => $user_openid,
    'remind_time' => $day_time,
    'remind_year' => $remind_year,
    'remind_month' => $remind_month,
    'remind_day' => $remind_day,
    'add_time' =>  date('Y-m-d H:i:s'),
    'remind_title' => $day_title,
    'remind_desc' => $day_desc,
    'recycle' => $recycle,
    'date_format' => $date_format
);

$mysqlidb_debug = 1;

// 先判断是否已经存在了
$db->where('account', $user_openid)->where('remind_title', $day_title);
$fetv = $db->has(TABLE_CLD_REMIND_INFO);

if($fetv == true)
{
    add_event_redirct_info("已经存在此名称的纪念日了，请重新输入", "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxab31157c01de5423&redirect_uri=http://hmtbzx.com/calendar/view/addevent.php&response_type=code&scope=snsapi_base&state=123&connect_redirect=1#wechat_redirect");
    return;
}

$ret = $db->insert(TABLE_CLD_REMIND_INFO, $special_day);

if($ret == false) {
    add_event_redirct_info("保存数据失败，请重试！", "../view/addevent_error.html");
}
else {
    add_event_redirct_info("纪念日添加成功！", "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxab31157c01de5423&redirect_uri=http://hmtbzx.com/calendar/index.php&response_type=code&scope=snsapi_base&state=123&connect_redirect=1#wechat_redirect");
}

?>